package com.fangfaze.java.hcwork.liveLifeCycle.controller;

import com.aliyuncs.live.model.v20161101.DescribeLiveStreamsNotifyUrlConfigResponse;
import com.fangfaze.java.hcwork.liveLifeCycle.dao.LiveCallbackDao;
import com.fangfaze.java.hcwork.liveLifeCycle.entity.Livecallback;
import com.fangfaze.java.hcwork.liveLifeCycle.entity.NetworkResult;
import com.fangfaze.java.hcwork.liveLifeCycle.entity.LiveUrls;
import com.fangfaze.java.hcwork.liveLifeCycle.dao.StreamPairDao;
import com.fangfaze.java.hcwork.liveLifeCycle.service.AliyunLiveService;
import com.fangfaze.java.hcwork.liveLifeCycle.utils.MessageSender;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;

@RestController
public class LiveController {

    @Autowired
    StreamPairDao streamPairDao;

    @Autowired
    LiveCallbackDao liveCallbackDao;

    @Autowired
    AliyunLiveService aliyunLiveService;

    @Autowired
    MessageSender messageSender;

    @GetMapping("")
    public String hello() {
        return "hello";
    }

    @GetMapping("/notify")
    public NetworkResult<DescribeLiveStreamsNotifyUrlConfigResponse.LiveStreamsNotifyConfig> getConfig() {
        return NetworkResult.Factory.success(aliyunLiveService.getConfig());
    }

    @GetMapping("/callback")
    public String callback(String time, String usrargs, String action, String app, String appname, String id, String node, String IP) {
        Livecallback livecallback = new Livecallback();
        livecallback.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
        livecallback.setTime(time);
        livecallback.setUsrargs(usrargs);
        livecallback.setAction(action);
        livecallback.setApp(app);
        livecallback.setAppname(appname);
        livecallback.setId(id);
        livecallback.setNode(node);
        livecallback.setIP(IP);

        liveCallbackDao.save(livecallback);

        return "1";
    }

    @GetMapping("test")
    public String test() {
        return "error";
    }


    @PostMapping("/stream/{id}")
    public NetworkResult<String> save(@PathVariable String id) {
        LiveUrls liveUrls = new LiveUrls();
        liveUrls.setId(id);
        streamPairDao.save(liveUrls);
        return NetworkResult.Factory.success();
    }

    @GetMapping("/stream/{id}")
    public NetworkResult<LiveUrls> stream(@PathVariable String id) {
        Optional<LiveUrls> result = streamPairDao.findById(id);
        try {
            return NetworkResult.Factory.success(result.get());
        } catch (NoSuchElementException e) {
            return NetworkResult.Factory.fail("400", "无数据");
        }
    }

    @GetMapping("/stream")
    public NetworkResult<List<LiveUrls>> streamList() {
        return NetworkResult.Factory.success(streamPairDao.findAll());
    }

    @GetMapping("/live")
    public NetworkResult<List<Livecallback>> live() {
        return NetworkResult.Factory.success(liveCallbackDao.findAll());

    }

}
